package com.xyj.week03;

import com.xyj.week01.Node;

/**
 * 反转链表
 * @author GuXiaoYao
 * @version 1.0.0
 * @since 2021/8/25
 */
public class T4 {


    public static void main(String[] args) {
        Node head=new Node(0);
        Node node1=new Node(1);
        Node node2=new Node(2);
        Node node3=new Node(3);
        Node node4=new Node(4);
        head.next=node1;
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        Node helpCur=head;
        System.out.println("反转前：");
        while (helpCur!=null){
         System.out.print(helpCur+" ");
         helpCur=helpCur.next;
        }
        Node node = reserveNode(head);
        System.out.println();
        System.out.println("反转后:");
        while (node!=null){
            System.out.print(node+" ");
            node=node.next;
        }
    }
    /**
     * 反转链表
     * @param head 头节点
     * @return 反转后的头节点
     */
    public static Node reserveNode(Node head){
        if (head==null){
            return null;
        }
        if (head.next==null){
            return head;
        }
        Node last = reserveNode(head.next);
        head.next.next=head;
        head.next=null;
        return last;
    }



}
